/*
 * Copyright 2011 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.db_vdumper.actions.view;

import java.awt.event.ActionEvent;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JMenuItem;
import net.infonode.util.Direction;
import org.opu.db_vdumper.actions.AbstractAction;
import org.opu.db_vdumper.ui.MainView;
import org.opu.db_vdumper.util.Logger;
import static org.opu.db_vdumper.ui.MainView.*;

/**
 *
 * @author yura
 */
public abstract class ViewCorrectAction extends AbstractAction {
    public static final String ENABLE_CLOSE = "EnableClose";
    public static final String FREZE_LAYOUT = "FrezeLayout";

    public static final String TOOGLE_DOWN = "ToogleDown";
    public static final String TOOGLE_LEFT = "ToogleLeft";
    public static final String TOOGLE_RIGHT = "ToogleRight";
    public static final String TOOGLE_UP = "ToogleUp";

    public static final Map<String, Direction> MAP;

    static {
        Map<String, Direction> map = new HashMap<String, Direction>();
        map.put(TOOGLE_DOWN, Direction.DOWN);
        map.put(TOOGLE_LEFT, Direction.LEFT);
        map.put(TOOGLE_RIGHT, Direction.RIGHT);
        map.put(TOOGLE_UP, Direction.UP);

        MAP = Collections.unmodifiableMap(map);
    }

    public static Map<String, ViewCorrectAction> allAvailable(final MainView mainView) {
        Map<String, ViewCorrectAction> actions = new HashMap<String, ViewCorrectAction>();

        addRestore(actions, mainView, TREE_TABLE_SCEMA_VIEW);
        addRestore(actions, mainView, GRAPH_TABLE_SCEMA_VIEW);
        addRestore(actions, mainView, SQL_QUERY_EDITOR_VIEW);
        addRestore(actions, mainView, DB_CONNECTION_PROP_VIEW);

        actions.put(ENABLE_CLOSE, new ViewCorrectAction() {

            @Override
            public String getName() {
                return ENABLE_CLOSE;
            }

            @Override
            public void action(ActionEvent e) {
                if (e.getSource() instanceof JMenuItem) {
                    mainView.setCloseButtonEnable(((JMenuItem) e.getSource()).isSelected());
                } else {
                    mainView.setCloseButtonEnable(mainView.isCloseButtonEnable());
                    serr(this, e.getSource());
                }
            }
        });

        actions.put(FREZE_LAYOUT, new ViewCorrectAction() {

            @Override
            public String getName() {
                return FREZE_LAYOUT;
            }

            @Override
            public void action(ActionEvent e) {
                if (e.getSource() instanceof JMenuItem) {
                    mainView.setFreezeLayout(((JMenuItem) e.getSource()).isSelected());
                } else {
                    mainView.setFreezeLayout(mainView.isFreezeLayout());
                    serr(this, e.getSource());
                }
            }
        });

        for (final String tgl : MAP.keySet()) {
            actions.put(tgl, new ViewCorrectAction() {

                @Override
                public String getName() {
                    return tgl;
                }

                @Override
                public void action(ActionEvent e) {
                    if (e.getSource() instanceof JMenuItem) {
                        mainView.setToogleEnabled(MAP.get(tgl),
                                ((JMenuItem) e.getSource()).isSelected());
                    } else {
                        mainView.replaceToogleEnabled(MAP.get(tgl));
                        serr(this, e.getSource());
                    }
                }
            });
        }

        return actions;
    }

    public static void addRestore(Map<String, ViewCorrectAction> map,
            final MainView mainView, final String name) {

        map.put(name, new ViewCorrectAction() {

            @Override
            public String getName() {
                return name;
            }

            @Override
            public void action(ActionEvent e) {
                mainView.restore(name);
            }
        });
    }

    private static void serr(Object c, Object o){
         Logger.getInstance().error(c, "Not toggle: " + o);
    }

}
